package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.dto.LoginUser;
import com.ktjy.mapper.SysUserMapper;
import com.ktjy.mapper.TbActivityMapper;
import com.ktjy.mapper.TbAssignRecordMapper;
import com.ktjy.mapper.TbClueMapper;
import com.ktjy.pojo.clues.TbAssignRecord;
import com.ktjy.pojo.clues.TbClue;
import com.ktjy.service.ITbActivityService;
import com.ktjy.service.ITbClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 线索管理Service业务层处理
 */
@Service
public class TbClueServiceImpl implements ITbClueService {


    @Autowired
    private TbClueMapper tbClueMapper;
    @Autowired
    TbAssignRecordMapper assignRecordMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private TbActivityMapper tbActivityMapper;


    @Autowired
    private ITbActivityService activityService;


    /**
     * 查询线索管理列表
     *
     * @param tbClue 线索管理
     * @return 线索管理
     */
    @Override
    public IPage<TbClue> selectTbClueList(TbClue tbClue, Integer pageNo, Integer pageSize) {
        Page<TbClue> page = new Page<>(pageNo, pageSize);
        return tbClueMapper.selectTbClueList(page, tbClue);
    }

    /**
     * 查询线索池管理列表
     *
     * @param tbClue
     * @return
     */

    @Override
    public IPage<TbClue> selectTbCluePool(TbClue tbClue, Integer pageNo, Integer pageSize) {
        Page<TbClue> page = new Page<>(pageNo, pageSize);
        return tbClueMapper.selectTbCluePoll(page, tbClue);
    }

    /**
     * 新增线索管理
     *
     * @param tbClue 线索管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTbClue(TbClue tbClue) {
        //获取当前登录 用户
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //1. 保存线索信息
        tbClue.setCreateBy(loginUser.getUsername());
        tbClue.setCreateTime(LocalDateTime.now());
        tbClueMapper.insertTbClue(tbClue);

        //2. 保存线索分配信息
        TbAssignRecord tbAssignRecord = new TbAssignRecord();
        tbAssignRecord.setAssignId(tbClue.getId());
        tbAssignRecord.setUserId(loginUser.getUserId());
        tbAssignRecord.setUserName(loginUser.getUsername());
        tbAssignRecord.setDeptId(loginUser.getDeptId());
        tbAssignRecord.setCreateBy(loginUser.getUsername());
        tbAssignRecord.setCreateTime(LocalDateTime.now());
        int rows = assignRecordMapper.insertAssignRecord(tbAssignRecord);

        return rows;
    }

    /**
     * 修改线索管理
     *
     * @param tbClue 线索管理
     * @return 结果
     */
    @Override
    public int updateTbClue(TbClue tbClue) {
        return tbClueMapper.updateTbClue(tbClue);
    }

    /**
     * 批量删除线索管理
     *
     * @param ids 需要删除的线索管理ID
     * @return 结果
     */
    @Override
    public int deleteTbClueByIds(Long[] ids) {
        return tbClueMapper.deleteTbClueByIds(ids);
    }

    /**
     * 删除线索管理信息
     *
     * @param id 线索管理ID
     * @return 结果
     */
    @Override
    public int deleteTbClueById(Long id) {
        return tbClueMapper.deleteTbClueById(id);
    }


}
